package pac;

public class KeyGen {

	private double keyOffset;
	
	public KeyGen (int lengthOfBitSet) {
		int numberOfDigitsForKey = 0;
		while( (double) lengthOfBitSet / Math.pow(10, numberOfDigitsForKey) > 1 )
			numberOfDigitsForKey ++;
		keyOffset = Math.pow(10, numberOfDigitsForKey);
	}
	
	public double makeKey(int[] variables) {
		double key = 0;
		for(int i=0; i<variables.length; i++)
			key = key*keyOffset + variables[i];
		return key;
	}
	
	public int[] breakKey(double key) {
		int numberOfVariables = 0;
		while ( (double) key / Math.pow(keyOffset, numberOfVariables) >= 1) 
			numberOfVariables++;
		int[] variables = new int[numberOfVariables];
		
		int varNo = numberOfVariables - 1;
		while ( (double) key / keyOffset > 0 ) {
			variables[varNo] = (int) ( key   -    Math.floor( ((double) key / keyOffset) ) * keyOffset );
			key = (long) Math.floor( ((double) key / keyOffset) );
			varNo--;
		}
			
		return variables;
	}
	
}
